package com.cfets.annualAssessment.backgroundController;

import com.cfets.annualAssessment.entity.RuleInq;
import com.cfets.annualAssessment.entity.TableContainer;
import com.cfets.annualAssessment.service.RuleInqService;
import lombok.extern.log4j.Log4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * Created by ShenYiChou on 2017/6/6.
 */
@Log4j
@Controller
@RequestMapping("/admin/RuleInq")
public class RuleInquire {
    @Autowired
    private RuleInqService ruleInqService;
    @RequestMapping("/index")
    public String index(Model model){
        return "RuleInq";
    }

    @RequestMapping(value = "/Get",method = RequestMethod.POST)
    public ResponseEntity<?> ruleInqList(RuleInq ruleInq){
        List<RuleInq> ruleInqList = ruleInqService.getRuleInqPage(ruleInq);
        TableContainer tableContainer = new TableContainer();
        tableContainer.setAaData(ruleInqList);
        tableContainer.setiTotalDisplayRecords(ruleInq.getTotalCount());
        tableContainer.setiTotalRecords(ruleInq.getTotalCount());
        return new ResponseEntity<>(tableContainer, HttpStatus.OK);
    }
    @RequestMapping(value = "/restful",method = RequestMethod.POST)
    public ResponseEntity<?> ruleInqlike(RuleInq ruleInq){
        return new ResponseEntity<>(true, HttpStatus.OK);
    }
    @RequestMapping(value = "/trunRuleInq",method = RequestMethod.GET)
    public ResponseEntity<?> trunRuleInq(){
        boolean flag = ruleInqService.trunRuleInq();
        return new ResponseEntity<>(flag, HttpStatus.OK);
    }

    @RequestMapping(value = "/import")
    public ResponseEntity<?> importExcel(MultipartFile mktFile) {
        try {
            Workbook xwb = WorkbookFactory.create(mktFile.getInputStream());
            Sheet sheet = xwb.getSheetAt(0);
            Row row;
            for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
                if (i == 0) {
                    continue;
                }
                row = sheet.getRow(i);
                RuleInq ruleInq = new RuleInq();
                if(Objects.equals(row.getCell(1).toString(), "")){
                    return new ResponseEntity<>(-1, HttpStatus.OK);//新增失败为空 -1
                }
                if(Objects.equals(row.getCell(2).toString(), "")){
                    return new ResponseEntity<>(-1, HttpStatus.OK);//新增失败为空 -1
                }
                if(Objects.equals(row.getCell(3).toString(), "")&&Objects.equals(row.getCell(4).toString(), "")){
                    return new ResponseEntity<>(-1, HttpStatus.OK);//新增失败为空 -1
                }
                ruleInq.setRULE_NAME(row.getCell(1).toString());
                ruleInq.setRULE_URL(row.getCell(2).toString());
                ruleInq.setRULE_TAG1(row.getCell(3).toString());
                ruleInq.setRULE_TAG2(row.getCell(4).toString());
                boolean flag = this.ruleInqService.newRuleInq(ruleInq);
                if(!flag) {
                    return new ResponseEntity<>(false, HttpStatus.OK);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(false, HttpStatus.OK);
        }
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    @PostMapping
    public ResponseEntity<?> createOrUpdate(@Valid RuleInq ruleInq, BindingResult result) {
        if (result.hasErrors()) {
            return new ResponseEntity<>(result.getAllErrors(), HttpStatus.OK);
        }
        boolean flag = false;
        if(ruleInq.getId()!=null && ruleInq.getId()!=0){
            flag = this.ruleInqService.updateRuleInq(ruleInq);
        }else {
            flag = this.ruleInqService.newRuleInq(ruleInq);
        }
        return new ResponseEntity<>(flag, HttpStatus.OK);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteRuleInq(@PathVariable("id") int id){
        boolean flag = this.ruleInqService.deleteRuleInqById(id);
        return new ResponseEntity<>(flag, HttpStatus.OK);
    }
}
